var swiper = new Swiper(".mySwiper", {
    loop:true,
	// autoplay: {
	// 	delay: 4000,
    // },
	navigation: {
		nextEl: ".swiper-button-next",
		prevEl: ".swiper-button-prev",
	},
    pagination: {
      el: ".swiper-pagination",
	  clickable :true,
    },on:{
			init:function(swiper){
		        slide=this.slides.eq(0);
			    slide.addClass('swiper-actives');
		    },
			transitionStart: function(){
			    for(i=0;i<this.slides.length;i++){
			    	slide=this.slides.eq(i);
			        slide.removeClass('swiper-actives');
				}
		    },
			transitionEnd: function(){
				slide=this.slides.eq(this.activeIndex);
			    slide.addClass('swiper-actives');
				
		    },
		}
  });


  $(".nav-li").on('mouseenter',function(){
    $(".left-subs-li").removeClass("left-subs-li-active");
    $(".left-subs-li1").addClass("left-subs-li-active")
})


$(".left-subs-li").on("mouseenter",function(){
	$(".left-subs-li").removeClass("left-subs-li-active");
    $(".left-subs-li1").addClass("left-subs-li-active")
}).mouseover(function(){
	if ($(this).hasClass("left-subs-li-active")){
        return;
    }else{
        $(".left-subs-li").removeClass("left-subs-li-active");
        $(this).addClass("left-subs-li-active")
    }
})

$(".change-li").on('click',function(){
	$(".change-li").removeClass('change-li-active')
	$(this).addClass("change-li-active")
	$(".prodcuts").removeClass("prodcuts-active")
	let ids = $(this).attr('id')
	$("#product-"+ids).addClass('prodcuts-active')
})

$(document).ready(function() {
	$(window).scroll(function() {
	  var scrollDistance = $(window).scrollTop();
	  var showDistance = 300; // 设置滚动距离阈值，控制何时显示元素

	  if (scrollDistance > showDistance) {
		$(".mobs-nav").addClass('mobs-nav-active');
	  } else {
		$(".mobs-nav").removeClass('mobs-nav-active');
	  }
	});
});

var newst = new Swiper(".newSwiper",{
    // 自动切换
    autoplay: {
      delay: 5000,
      disableOnInteraction: false,
    },
    // 循环
    loop: true,
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    }
})


if ($("#allmap").length>0){
    var map = new BMapGL.Map("allmap");
    var point = new BMapGL.Point(113.984266,22.624347);
    map.centerAndZoom(point, 16);
    map.enableScrollWheelZoom(true);
    var marker = new BMapGL.Marker(point);  // 创建标注
    map.addOverlay(marker);              // 将标注添加到地图中
    var opts = {
        width : 200,     // 信息窗口宽度
        height: 100,     // 信息窗口高度
        title : "深圳市峰华科技有限公司" , // 信息窗口标题
        message:"深圳市南山区西丽大磡村工业二路大磡科技园二期A栋五楼"
    }
    var infoWindow = new BMapGL.InfoWindow("地址：深圳市南山区西丽大磡村工业二路大磡科技园二期A栋五楼", opts);  // 创建信息窗口对象 
    map.openInfoWindow(infoWindow, point); //开启信息窗口
    marker.addEventListener("click", function(){          
        map.openInfoWindow(infoWindow, point); //开启信息窗口
    }); 
}


//获取验证码
function getcode(){
    $.get('/apis/captcha?rand='+ Math.random(),function(res){
        $("#codess").attr('src',res);
    })
 
}



//submit form
function quere(){
    var name = $("#name").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var code = $("#code").val();
    var inquiry = $("#inquiry").val();
    var token = $("#token").val();
    if(cheeck(["name","phone","email","inquiry","code"])){
        return 0;
    }
    $.post("/apis/feedback",{_token:token,name:name,phone:phone,email:email,code:code,inquiry:inquiry},function(res){
        if (res.code===200){
            clock('提交成功','提交成功，我们会尽快通过电话或邮件联系您')
            $("#name").val('');
            $("#phone").val('');
            $("#email").val('');
            $("#code").val('');
            $("#inquiry").val('');
        }else{

            clock('提交失败',res.msg)
        }
    })
   
}


function clock(title,desc){
    $(".success-title").text(title)
    $(".success-desc").text(desc)
    $(".success").css('display','block')
    setTimeout(function(){
        $(".success").css('display','none')
    },2000)
}


$('input').on('change', function() {
    var element = $(this).attr("id")
    if(element=="email"){
        $("#email-error").addClass("hidden");
        $("#email").removeClass("border-red-600");
        const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        if (!regex.test($(this).val())){
            $("#"+element+"-invalid-error").removeClass("hidden");
            $("#email").addClass("border-red-600")
        }else{
            $("#email-invalid-error").addClass("hidden");
        }
    }else if(element=="phone"){
        const phones = /^(0\d{2,3}-?\d{7,8})$|^((\+|00)86)?1[3-9]\d{9}$/
        if (!phones.test($("#phone").val())){
            $("#"+element+"-invalid-error").removeClass("hidden");
            $("#phone").addClass("border-red-600")   
        }else{
            $("#phone").removeClass("border-red-600") 
            $("#phone-invalid-error").addClass("hidden"); 
        }
    }
    else{
        $("#"+element).removeClass("border-red-600")
        $("#"+element+"-error").addClass("hidden")
    }
    
});
function cheeck(cheekList){
    var cheeck = false;
    cheekList.forEach(element => {
       if($("#"+element).val()===""){
            $("#"+element+"-error").removeClass("hidden");
            $("#"+element).addClass("border-red-600")
            cheeck = true;
       }else{
            if(element==="email"){
                const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                if (!regex.test($("#email").val())){
                    $("#"+element+"-invalid-error").removeClass("hidden");
                    $("#email").addClass("border-red-600")
                    $("#email").focus()
                    cheeck = true;
                }
            }else if(element==="phone") {
                const phones = /^(0\d{2,3}-?\d{7,8})$|^((\+|00)86)?1[3-9]\d{9}$/
                if (!phones.test($("#phone").val())){
                    $("#"+element+"-invalid-error").removeClass("hidden");
                    $("#phone").addClass("border-red-600")
                    $("#phone").focus()
                    cheeck = true;
                }
            }
       }
       
    });
    return cheeck;
}
